function [L,num]=laIel(img)
I=imread('sandstone6erzhi.png'); 
[m,n]=size(I);
subplot(121);
imshow(I);
title('原始图像');

for  x = 1:1:64
	 for y=1:1:n
		if( I(x,y) < 165)
			I(x,y) = 0;
		else
			I(x,y) = 255;
        end
     end
end 
for  x = 65:1:128
	 for y=1:1:n
		if( I(x,y) < 178)
			I(x,y) = 0;
		else
			I(x,y) = 255;
        end
     end
end  
for  x = 129:1:194
	 for y=1:1:n
		if( I(x,y) < 150)
			I(x,y) = 0;
		else
			I(x,y) = 255;
        end
     end
end  
for  x = 195:1:m
	 for y=1:1:n
		if( I(x,y) < 87)
			I(x,y) = 0;
		else
			I(x,y) = 255;
        end
     end
end   


L=zeros(size(I));
laIel=1;

for i=1:m
    for j=1:n
        if j-1>0
            if I(i,j-1)==I(i,j)&&I(i,j)>0
                L(i,j)=L(i,j-1);
            end
        end
        if i-1>0
            if I(i-1,j)==I(i,j)&&I(i,j)>0
                L(i,j)=L(i-1,j);
                if j-1>0&& I(i,j-1)==I(i,j)&&L(i,j-1)~=L(i-1,j)
                   L(find(L==L(i,j-1)))=L(i-1,j);
                   laIel=laIel-1;
                end
            end
         end
            
        if L(i,j)==0&&I(i,j)>0
            L(i,j)=laIel;
            laIel=laIel+1;
        end       
    end
end


num=max(max(L)); 
disp('米粒个数:');
disp(num);
subplot(122);
imshow(L);
title(sprintf('%s %d','米粒个数=',num));






